package com.fany.views.circlemenu;

import java.util.ArrayList;

import com.fany.utils.AlgorithmUtils;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.Rect;
import com.fany.utils.BitmapHelper;

public class CircleMenuItem
{
    private static final int bitmapPixels = 96;
    private CircleArea _area;

    public CircleMenuItem(CircleArea area)
    {
        _area = area;
        _children = new ArrayList<CircleMenuItem>();
    }

    private Bitmap _bitmap;

    public void setBitmap(Bitmap bitmap)
    {
        _bitmap = BitmapHelper.toRoundCorner(bitmap, bitmapPixels);
    }

    public Bitmap getBitmap()
    {
        return _bitmap;
    }

    public int getAngle()
    {
        return _angle;
    }

    public void setAngle(int angle)
    {
        if (angle > 0)
        {
            this._angle = angle % 360;
        }
        else
        {
            this._angle = angle + 360;
        }
    }

    private int _angle;

    public float x;

    public float y;

    public String text;

    public boolean visible = true;

    public int commandId;

    public boolean focused;

    public boolean isPointInMenu(PointF touchedPoint)
    {
        int widthArea = bitmapPixels / 2;
        int heightArea = bitmapPixels / 2;
        return (touchedPoint.x >= x - widthArea) && (touchedPoint.x <= x + widthArea)
                && (touchedPoint.y >= y - heightArea) && (touchedPoint.y <= y + heightArea);
    }

    public void adjustItemCoordinate()
    {
        PointF point = AlgorithmUtils.calculateCirclePoint(_area.centerPoint, _area.radius, _angle);
        x = point.x;
        y = point.y;
    }

    public Rect getItemRect()
    {
        Rect rect = new Rect();
        rect.left = (int) (x - bitmapPixels / 2);
        rect.right = (int) (x + bitmapPixels / 2);
        rect.top = (int) (y - bitmapPixels / 2);
        rect.bottom = (int) (y + bitmapPixels / 2);
        return rect;
    }

    private ArrayList<CircleMenuItem> _children;

    public void addChild(CircleMenuItem item)
    {
        _children.add(item);
    }

    public void removeChild(CircleMenuItem item)
    {
        _children.remove(item);
    }

    public void removeChild(int index)
    {
        _children.remove(index);
    }

    public int getChildCount()
    {
        return _children.size();
    }
}